package com.hyz.service.assetManager.impl;

import com.alibaba.fastjson.JSON;
import com.hyz.client.AssetClient;
import com.hyz.pojo.ResponseResult;
import com.hyz.pojo.assetManager.AmOperationEnum;
import com.hyz.service.assetManager.AssetManagerService;
import com.hyz.util.GetValue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.asset.asset.Asset;
import pojo.asset.asset.AssetStatusEnum;
import pojo.permission.assetManager.AssetImportRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author HGD
 * @date 2022/10/2 17:22
 */
@Service
public class AssetManagerServiceImpl implements AssetManagerService {
    @Autowired
    private GetValue getValue;
    @Autowired
    private AssetClient assetClient;
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult<List<Asset>> getUnassignedAsset() {
        List<Asset> assets = assetClient.selectAssetByStatus(AssetStatusEnum.UNASSIGNED.getKey().longValue());
        if (assets == null) {
            return new ResponseResult<>(200, "无未分库资产", null);
        }
        return new ResponseResult<>(200, "返回未分库资产", assets);
    }

    @Override
    public ResponseResult<String> createAssetImport(AssetImportRequest request) {
        // TODO: 通过 SpringSecurity 获取资产管理员id
        Long assetManagerId = getValue.getUuid();
        request.setAssetManagerId(assetManagerId);
        // TODO: 通过 Spring Security 获取用户职责
        String right = "assetManager";
        String routingKey = AmOperationEnum.createAssetImport.getValue();
        rabbitTemplate.convertAndSend(right, routingKey, JSON.toJSONString(request));
        return new ResponseResult<>(200, "创建入库申请，正在审核", null);
    }
}
